﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DevelopersToolbox {
    public partial class BinaryOrStringEditor : UserControl {
        private bool _textInputAllowed;
        private bool _binaryInputAllowed;
        public event EventHandler DataChanged;

        public BinaryOrStringEditor() {
            InitializeComponent();
            _textInputAllowed = true;
            _binaryInputAllowed = true;
        }

        public override string Text {
            get {
                return GetDataAsString();
            }
            set {
                Data = Encoding.ASCII.GetBytes(value);
            }
        }

        public override Font Font {
            get {
                return base.Font;
            }
            set {
                base.Font = value;
                _text.Font = value;
                _binary.Font = value;
            }
        }

        [DefaultValue(true)]
        public bool TextInputAllowed {
            get { return _textInputAllowed; }
            set {
                EnableDisableFormatSelection(value);
                _dataFormatText.Checked = value;
                _dataFormatBinary.Checked = !value;
                _textInputAllowed = value;
            }
        }

        [DefaultValue(true)]
        public bool BinaryInputAllowed {
            get { return _binaryInputAllowed; }
            set {
                EnableDisableFormatSelection(value);
                _dataFormatBinary.Checked = value;
                _dataFormatText.Checked = !value;
                _binaryInputAllowed = value;
            }
        }

        private void EnableDisableFormatSelection(bool value) {
            _dataFormatBinary.Visible = value;
            _dataFormatText.Visible = value;
            if (value) {
                _binary.Top = _dataFormatBinary.Bottom + Margin.Top;
                _binary.Height = Height - (_dataFormatBinary.Bottom + Margin.Bottom);
                _text.Top = _dataFormatBinary.Bottom + Margin.Top;
                _text.Height = Height - (_dataFormatBinary.Bottom + Margin.Bottom);
            } else {
                _binary.Top = Margin.Top;
                _binary.Height = Height - (Margin.Top + Margin.Bottom);
                _text.Top = Margin.Top;
                _text.Height = Height - (Margin.Top + Margin.Bottom);
            }
        }

        public byte[] Data {
            get {
                if (_dataFormatText.Checked) {
                    return Encoding.ASCII.GetBytes(_text.Text);
                } else {
                    return _binary.Data;
                }
            }
            set {
                if (_dataFormatText.Checked) {
                    _text.Text = Encoding.ASCII.GetString(value);
                } else {
                    _binary.Data = value;
                }
            }
        }

        public string GetDataAsString() {
            return Encoding.ASCII.GetString(Data);
        }

        private void _dataFormatText_CheckedChanged(object sender, EventArgs e) {
            if (_dataFormatText.Checked) {
                _dataFormatBinary.Checked = false;
                _text.Text = Encoding.ASCII.GetString(_binary.Data);
                SuspendLayout();
                _binary.Visible = false;
                _text.Visible = true;
                ResumeLayout(true);
            }
        }

        private void _dataFormatBinary_CheckedChanged(object sender, EventArgs e) {
            if (_dataFormatBinary.Checked) {
                _dataFormatText.Checked = false;
                _binary.Data = Encoding.ASCII.GetBytes(_text.Text);
                SuspendLayout();
                _text.Visible = false;
                _binary.Visible = true;
                ResumeLayout(true);
            }
        }

        private void _text_TextChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged(sender, e);
            }
        }

        private void _binary_DataChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged(sender, e);
            }
        }

        private void _dataFormatText_Click(object sender, EventArgs e) {
            _dataFormatText.Checked = true;
        }

        private void _dataFormatBinary_Click(object sender, EventArgs e) {
            _dataFormatBinary.Checked = true;
        }

        private void BinaryOrStringEditor_Load(object sender, EventArgs e) {
            if (!_binaryInputAllowed) {
                _binary.Visible = false;
            }
            if (!_textInputAllowed) {
                _text.Visible = false;
            }
        }
    }
}
